﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DTOMall;
using DAOMall;
using BLLMall;
namespace MallProject.Admin.View
{
    public partial class User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if (IsPostBack == false)
            {

                Databound();

            }
        }
        public void Databound()
        {
            UsersDTO user = new UsersDTO();
            dgrUser.DataSource = user.getAll();
            dgrUser.DataBind();

        }

        protected void dgrUser_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            UsersDTO user = new UsersDTO();
            Users u = new Users();
            string command = e.CommandName;
            int UserId = Convert.ToInt32(e.CommandArgument);
            if (command.Equals("Edit"))
            {

                MultiView1.ActiveViewIndex = 1;
                u = (Users)user.getByID(UserId);
                txtID.Text = u.id.ToString();
                txtUser.Text = u.username;
                txtPhone.Text = u.phone;
                txtFname.Text = u.fullname;
                txtEmail.Text = u.email;
                txtAdd.Text = u.address;
                txtSkype.Text = u.skype;
                txtYahoo.Text = u.yahoo;
                int role = u.group_id;
                drlposition.SelectedIndex = u.group_id - 1;
                txtBirth.Text = String.Format("{0:d}", u.birthday);
            }
            else {
                if(command.Equals("Delete")){
                    Boolean check = user.delete(UserId);
                    String Notic = "";
                    Notic = (check == true) ? "Delete succes" : "Cannot delete this item";
                    Response.Write("<script>alert('" + Notic + "')</script>");
                    Databound();
                    MultiView1.ActiveViewIndex = 0;
                }
            }
        }

        protected void dgrUser_ItemDataBound(object sender, DataGridItemEventArgs e)
        {

        }

        protected void dgrUser_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            dgrUser.CurrentPageIndex = e.NewPageIndex;
            Databound();
        }


        protected void btnBack_Click1(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 0;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            UsersDTO user = new UsersDTO();
            Users u = new Users();
            u.id = Convert.ToInt32(txtID.Text);
            u.username = txtUser.Text;
            u.fullname = txtFname.Text;
            u.phone = txtPhone.Text;
            u.yahoo = txtYahoo.Text;
            u.address = txtAdd.Text;
            u.email = txtEmail.Text;
            u.skype = txtSkype.Text;
            u.modified_date = DateTime.Now;
            u.group_id = drlposition.SelectedIndex + 1;
            //object obj = (object)u;
            bool check=user.update(u);
            Response.Write("<script>alert('"+check+"');</script>");

            MultiView1.ActiveViewIndex = 0;
        }

        protected void Addnew_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 2;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            UsersDTO user = new UsersDTO();
            Users u = new Users();
            u.username = txtu.Text;
            u.password = txtp.Text;
            u.fullname = txtf.Text;
            u.phone = txtph.Text;
            u.yahoo = txtym.Text;
            u.address = txtadr.Text;
            u.email = txte.Text;
            u.skype = txtsk.Text;
            u.created_date = DateTime.Now;
            u.group_id = drlpos.SelectedIndex + 1;
            u.birthday = DateTimeLib.Convert2Date(TextBox1.Text);
            user.insert(u);
        }
    }
}